<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include ('../global/initialize.php');

aos_header('Horse races');
?>
					<div class="blok">
<?php
printDiplomacyTab();
?> 
					</div>
					<br />
<?php
$current_turn = getCurrentTurn();
$query = "SELECT race_id " .
"FROM races " .
"WHERE turn >= $current_turn " .
"ORDER BY turn DESC " .
"LIMIT 1";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$next_race_id = $row['race_id'];

$race_team_id = -1;
if (isset ($_GET['team_id'])) {
	$race_team_id = formatInput($_GET['team_id']);
}
if (is_numeric($race_team_id)) {
	$query = "SELECT * FROM race_teams WHERE race_team_id = $race_team_id";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if (isset ($row['race_team_id'])) {
		$morale = getMoraleString($row['morale']);

		$query2 = "SELECT count(*) FROM race_history WHERE race_team_id = $race_team_id";
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$races = $row2['count(*)'];

		if ($races > 0) {
			$query2 = "SELECT SUM(rank) FROM race_history WHERE race_team_id = $race_team_id";
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$avg_rank = $row2['SUM(rank)'] / $races;

			$query2 = "SELECT count(*) FROM race_history WHERE race_team_id = $race_team_id AND rank = 1";
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$wins = $row2['count(*)'];
			$won = round($wins / $races * 100.0) . '%';
		} else {
			$avg_rank = "-";
			$won = "-";
			$wins = "-";
		}

		$query2 = "SELECT SUM(gold_won) FROM race_history WHERE race_team_id = $race_team_id";
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$gold_won = number_format($row2['SUM(gold_won)']);

		$level = floor(($row['acceleration'] + $row['agility'] + $row['stamina'] + $row['strength'] + $row['intelligence']) / 5.0);
?>
				<div class="blok">
					<?php transprint( 'Horse race team' ); ?>: <strong><?php print( $row[ 'name' ] ); ?></strong><br /><br />
					<?php transprint( 'Races' ); ?>: <?php print( number_format( $races ) ); ?><br />
					<?php transprint( 'Wins' ); ?>: <?php print( number_format( $wins ) ); ?><br />
					<?php transprint( '1st prize' ); ?>: <?php print( $won ); ?><br />
					<?php transprint( 'Average rank' ); ?>: <?php print( round( $avg_rank, 2 ) ); ?><br />
					<?php transprint( 'Prize gold' ); ?>: <strong class="gold"><?php print( $gold_won ); ?></strong><br /><br />
					<strong><?php transprint( 'Team attributes' ); ?></strong><br />
					<?php transprint( 'Acceleration' ); ?>: <?php print( number_format( $row[ 'acceleration' ] ) ); ?><br />
					<?php transprint( 'Agility' ); ?>: <?php print( number_format( $row[ 'agility' ] ) ); ?><br />
					<?php transprint( 'Intelligence' ); ?>: <?php print( number_format( $row[ 'intelligence' ] ) ); ?><br />
					<?php transprint( 'Stamina' ); ?>: <?php print( number_format( $row[ 'stamina' ] ) ); ?><br />
					<?php transprint( 'Strength' ); ?>: <?php print( number_format( $row[ 'strength' ] ) ); ?><br /><br />
					<?php transprint( 'Overall team quality' ); ?>: <?php print( number_format( $level ) ); ?><br /><br />
					<?php transprint( 'Morale' ); ?>: <?php print( $morale ); ?><br />
					<br />
					<form action="place_bet.php" method="post">
						<input type="hidden" name="team_id" value="<?php print( $row[ 'race_team_id' ] ); ?>" />
						<input type="submit" value="<?php transprint( 'Place bet' ); ?>" />
					</form>
<?php

		$query = "SELECT race_history.*, race_teams.name, races.date " .
		"FROM race_history " .
		"JOIN race_teams ON race_teams.race_team_id = race_history.race_team_id " .
		"JOIN races ON races.race_id = race_history.race_id " .
		"WHERE race_history.race_team_id = $race_team_id AND turn <= $current_turn " .
		"ORDER BY turn " .
		"DESC LIMIT 0, 5";
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$num = mysql_numrows($result);
		if ($num > 0) {
			print "<br /><br /><table id=\"twothird\"><th colspan=\"3\">" . translate('Results from last') . " $num " . translate('races') . "</th>\r\n";
			print "<tr><td align=\"right\">" . translate('Rank') . "</td><td align=\"right\">" . translate('Date') . "</td><td align=\"right\">" . translate('Prize gold') . "</td></tr>\r\n";
			$i = 0;
			while ($i < $num) {
				if ($i % 2 == 0) {
					print "<tr class=\"even\">";
				} else {
					print "<tr>";
				}
				print ("<td align=\"right\">" . mysql_result($result, $i, "rank") . "</td>");
				print ("<td align=\"right\"><a href=\"race.php?race_id=" . mysql_result($result, $i, "race_id") . "\">" . mysql_result($result, $i, "date") . "</a></td>");
				print ("<td align=\"right\"><strong class=\"gold\">" . number_format(mysql_result($result, $i, "gold_won")) . "</strong></td></tr>\r\n");
				$i++;
			}
			print ("</table>\r\n\r\n");
		}
?> 
				</div> <!-- close blok -->
				<br />
<?php

	}
}

if (isset ($next_race_id) && $next_race_id > 0) {
	$query = "SELECT SUM(gold) FROM race_bets WHERE race_id = $next_race_id";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$total_bets = $row['SUM(gold)'];
	// TODO: if there get more then 50, add paging
} else {
	$total_bets = 0;
}
$query = "SELECT * FROM race_teams ORDER BY morale DESC LIMIT 0, 50";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$num = mysql_numrows($result);
if ($num > 0) {
	print "<table><th colspan=\"7\">" . translate('Horse race teams by morale') . "</th>\r\n";
	print "<tr><td>" . translate('Morale') . "</td><td>" . translate('Name') . "</td><td>" . translate('Races won') . "</td><td>" . translate('Payout') . "</td><td>" . translate('My bets') . "</td><td>" . translate('Total bets') . "</td><td>" . translate('Place bet') . "</td></tr>\r\n";
	$i = 0;
	while ($i < $num) {
		if ($i % 2 == 0) {
			print "<tr class=\"even\">";
		} else {
			print "<tr>";
		}

		$morale = getMoraleString(mysql_result($result, $i, "morale"));
		print ("<td>$morale</td>");
		print ("<td><a href=\"raceteam.php?team_id=" . mysql_result($result, $i, "race_team_id") . "\">" . mysql_result($result, $i, "name") . "</a></td>");
		$query2 = "SELECT count(*) FROM race_history WHERE race_team_id = " . mysql_result($result, $i, "race_team_id");
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		$races = $row2['count(*)'];
		$won = 0;

		if ($races > 0) {
			$query2 = "SELECT count(*) " .
			"FROM race_history " .
			"WHERE rank = 1 AND race_team_id = " . mysql_result($result, $i, "race_team_id");
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$won = $row2['count(*)'];

			//print( "avg_win: $avg_win <br />" );
			$odds = pow(1.1, $races / max(1, $won));
			if ($won == 0) {
				$avg_win = '0%';
			} else {
				$avg_win = round(($won / $races) * 100.0, 2) . '%';
			}
		} else {
			$odds = 2;
			$avg_win = "-";
		}

		$odds = round($odds, 2) . ":1";

		if (isset ($next_race_id) && $next_race_id > 0) {
			$query2 = "SELECT SUM(gold) FROM race_bets WHERE race_id = $next_race_id AND race_team_id = " . mysql_result($result, $i, "race_team_id") . " AND ruler_id = " . $_SESSION['ruler_id'];
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$own_bets = $row2['SUM(gold)'];
		} else {
			$own_bets = -1;
		}

		if ($own_bets > 0) {
			$own_bets = number_format($own_bets);
		} else {
			$own_bets = "-";
		}

		if (isset ($next_race_id) && $next_race_id > 0) {
			$query2 = "SELECT SUM(gold) FROM race_bets WHERE race_id = $next_race_id AND race_team_id = " . mysql_result($result, $i, "race_team_id");
			$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			$total_bets_team = $row2['SUM(gold)'];
		} else {
			$total_bets_team = -1;
		}

		if ($total_bets_team > 0) {
			$total_bets_team = number_format($total_bets_team);
		} else {
			$total_bets_team = "-";
		}

		print ("<td align=\"right\">$avg_win</td>");
		print ("<td align=\"right\">$odds</td>");
		print ("<td align=\"right\"><strong class=\"gold\">$own_bets</strong></td>");
		print ("<td align=\"right\"><strong class=\"gold\">$total_bets_team</strong></td>");
		print ("<td>" .
		"<form action=\"place_bet.php\" method=\"post\">" .
		"<input type=\"hidden\" name=\"team_id\" value=\"" . mysql_result($result, $i, "race_team_id") . "\" />" .
		"<input type=\"submit\" value=\"" . translate('Place bet') . "\" />" .
		"</form>" .
		"</td></tr>");
		$i++;
	}
	print ('</table>');
} else {
?>
				<blok class="blok">
					<?php transprint( 'There are no horse race teams.' ); ?>
				</blok>
	<?php

}
aos_footer();
?>